package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.qiantu.api.entity.DeviceModeValue;
import com.qiantu.api.entity.UniversalDeviceAction;
import com.qiantu.api.entity.UniversalDeviceData;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.adapter.SelectRoomFloorAdapter;
import com.qiantu.phone.ui.adapter.UniversalConfigAdapter;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class UniversalConfigActvity extends AppActivity {
    private RecyclerView list;
    private UniversalConfigAdapter adapter;
    private String deviceSerialNo;
    private List<List<UniversalDeviceData>> actionDtoList;
    private List<UniversalDeviceData> otherActionDtos;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_floor_room;
    }

    @Override
    protected void initView() {
        String title = getString("title");
        if(!TextUtils.isEmpty(title)){
            setTitle(title);
        }
        deviceSerialNo = getString("deviceSerialNo");
        list = findViewById(R.id.list);
        list.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new UniversalConfigAdapter(getContext());
        list.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        LLHttpManager.getDeviceTgpactions((LifecycleOwner) getContext(), deviceSerialNo, new HttpCallback<HttpData<UniversalDeviceAction>>(null) {
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<UniversalDeviceAction> result) {

                rebuildData(result.getData().getTgpActions());
            }

            @Override
            public void onFail(Exception e) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ToastUtils.show(R.string.exec_failure);
                }
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        });
    }

    private void rebuildData(List<UniversalDeviceData> actionDtos){
        actionDtoList = new ArrayList<>();
        otherActionDtos = new ArrayList<>();
        Map<String,List<UniversalDeviceData>> map = new HashMap();
        for(UniversalDeviceData actionDto : actionDtos){
            if(actionDto.getActionIdentifier().equals("power")){
                otherActionDtos.add(actionDto);
                continue;
            }
            if(map.containsKey(actionDto.getActionIdentifier())){
                map.get(actionDto.getActionIdentifier()).add(actionDto);
            }else{
                List<UniversalDeviceData> actions = new ArrayList<>();
                actions.add(actionDto);
                map.put(actionDto.getActionIdentifier(),actions);
            }
        }
        for(List<UniversalDeviceData> actions : map.values()){
            actionDtoList.add(actions);
        }
        actionDtoList.sort(new Comparator<List<UniversalDeviceData>>() {
            @Override
            public int compare(List<UniversalDeviceData> o1, List<UniversalDeviceData> o2) {
                return o1.get(0).getSort() < o2.get(0).getSort() ? -1 : 1;
            }
        });
        for(List<UniversalDeviceData> actions : actionDtoList){
            actions.sort(new Comparator<UniversalDeviceData>() {
                @Override
                public int compare(UniversalDeviceData o1, UniversalDeviceData o2) {
                    return o1.getSort() < o2.getSort() ? -1 : 1;
                }
            });
        }
        adapter.setData(actionDtoList);
    }

    @Override
    public void onRightClick(View view) {
        List<UniversalDeviceData> actionDtos = new ArrayList<>();
        actionDtos.addAll(otherActionDtos);
        for(List<UniversalDeviceData> dtos : actionDtoList){
            actionDtos.addAll(dtos);
        }
        LLHttpManager.saveDeviceTgpactions((LifecycleOwner) getContext(), actionDtos, AppApplication.getInstance().getCurrentHouseSerialNo(), new HttpCallback<HttpData<UniversalDeviceAction>>(null) {
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<UniversalDeviceAction> result) {
                finish();
            }

            @Override
            public void onFail(Exception e) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ToastUtils.show(R.string.exec_failure);
                }
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        });
    }


}
